﻿#include <stdio.h>
#include <string.h>

#define MUX_MODE 4 // 1:TDM 2:STDM 3:FDM 4:CDM

#define A_FREQUENCY 1
#define B_FREQUENCY 2

/* 多路复用实现（通过宏切换模式）*/
int multiplex(unsigned char* c, const int c_size,
    const unsigned char* a, const int a_len,
    const unsigned char* b, const int b_len)
{
#if MUX_MODE == 1 // 同步时分复用
    if (c_size < a_len + b_len) return -1;
    int k = 0;
    for (int i = 0; i < a_len || i < b_len; ++i)
    {
        if (i < a_len) c[k++] = a[i];
        else c[k++] = 'N';
        if (i < b_len) c[k++] = b[i];
        else c[k++] = 'N';
    }
    return k;

#elif MUX_MODE == 2 // 统计时分复用
    int k = 0;
    int a_idx = 0, b_idx = 0;
    for (int i = 0; i < a_len / A_FREQUENCY || i < b_len / B_FREQUENCY; ++i)
    {
        int a_frequency = A_FREQUENCY;
        int b_frequency = B_FREQUENCY;
        while (a_frequency--)
        {
            if (a_idx < a_len) c[k++] = a[a_idx++];
            else c[k++] = 'N';
        }
        while (b_frequency--)
        {
            if (b_idx < b_len) c[k++] = b[b_idx++];
            else c[k++] = 'N';
        }
    }
    return k;

#elif MUX_MODE == 3 // 频分复用
    if (c_size < 2 * (a_len > b_len ? a_len : b_len)) return -1;
    for (int i = 0; i < a_len; ++i) c[2 * i] = a[i];   // 偶数为通道A
    for (int i = 0; i < b_len; ++i) c[2 * i + 1] = b[i]; // 奇数为通道B
    return 2 * (a_len > b_len ? a_len : b_len);

#elif MUX_MODE == 4 // 码分复用
    // 使用正交沃尔什码：A用[1,1], B用[1,-1]
    if (c_size < 2 * (a_len > b_len ? a_len : b_len)) return -1;
    for (int i = 0; i < a_len || i < b_len; ++i)
    {
        int a_val = (i < a_len && a[i] == '1') ? 1 : 0;
        int b_val = (i < b_len && b[i] == '1') ? 1 : 0;
        c[2 * i] = '0' + a_val * 1 + b_val * 1; // 码片1
        c[2 * i + 1] = '0' + a_val * 1 + b_val * 0; // 码片2（归一化为0/1）
    }
    return 2 * (a_len > b_len ? a_len : b_len);
#endif
}

/* 多路解复用实现 */
int demultiplex(unsigned char* a, const int a_size,
    unsigned char* b, const int b_size,
    const unsigned char* c, const int c_len)
{
#if MUX_MODE == 1 // TDM解复用
    int a_idx = 0, b_idx = 0;
    for (int i = 0; i < c_len; i += 2)
    {
        if (a_idx < a_size) a[a_idx++] = (i < c_len) ? c[i] : '\0';
        if (b_idx < b_size) b[b_idx++] = (i + 1 < c_len) ? c[i + 1] : '\0';
    }
    return 0;

#elif MUX_MODE == 2 // STDM解复用
    int a_idx = 0, b_idx = 0;

    for (int i = 0; i < c_len; i += A_FREQUENCY + B_FREQUENCY)
    {
        int a_frequency = A_FREQUENCY;
        int b_frequency = B_FREQUENCY;
        while (a_frequency--)
            if (a_idx < a_size) a[a_idx++] = (i < c_len) ? c[i] : '\0';
        while (b_frequency--)
            if (b_idx < b_size) b[b_idx++] = (i + B_FREQUENCY - b_frequency < c_len) ? c[i + B_FREQUENCY - b_frequency] : '\0';
    }
    return 0;

#elif MUX_MODE == 3 // FDM解复用
    for (int i = 0; i < a_size; ++i)
        a[i] = (2 * i < c_len) ? c[2 * i] : '\0';
    for (int i = 0; i < b_size; ++i)
        b[i] = (2 * i + 1 < c_len) ? c[2 * i + 1] : '\0';
    return 0;

#elif MUX_MODE == 4 // CDM解复用
    for (int i = 0; i < a_size || i < b_size; ++i) {
        // A通道解码
        int a_sum = 0;
        if (2 * i < c_len) a_sum += c[2 * i] - '0';   // 码片1
        if (2 * i + 1 < c_len) a_sum += c[2 * i + 1] - '0'; // 码片2
        if (i < a_size) a[i] = (a_sum > 1) ? '1' : '0';

        // B通道解码
        int b_sum = 0;
        if (2 * i < c_len) b_sum += c[2 * i] - '0';   // 码片1
        if (2 * i + 1 < c_len) b_sum -= c[2 * i + 1] - '0'; // 码片2
        if (i < b_size) b[i] = (b_sum == 1) ? '1' : '0';
    }
    return 0;
#endif
}

int main() {
    // 测试数据
    unsigned char a[] = { "101" }; // 序列A
    unsigned char b[] = { "011011" }; // 序列B
    unsigned char c[20] = { 0 };

    // 复用测试
    int c_len = multiplex(c, 20, a, 3, b, 6);
    if (c_len == -1)
    {
        printf("Buffer overflow!\n");
        return -1;
    }

    // 解复用测试
    unsigned char a_out[10] = { 0 };
    unsigned char b_out[10] = { 0 };
    demultiplex(a_out, 3, b_out, 6, c, c_len);

    // 打印结果
    printf("Original A: "); for (int i = 0; i < 3; i++) printf("%c ", a[i]); puts("");
    printf("Original B: "); for (int i = 0; i < 6; i++) printf("%c ", b[i]); puts("");
    printf("Merge C: ");    for (int i = 0; i < c_len; i++) printf("%c ", c[i]); puts("");
    printf("Demux A   : "); for (int i = 0; i < 3; i++) printf("%c ", a_out[i]); puts("");
    printf("Demux B   : "); for (int i = 0; i < 6; i++) printf("%c ", b_out[i]); puts("");

    return 0;
}